<?php

    // +----------------------------------------------------------------------
    // | User的event操作
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\event\ucenter;

    use app\event\ucenter\UserExperience as UserExperienceEvent;
	use think\facade\Config;
	use think\facade\Db;
    use app\common\event\Base;
    use app\model\ucenter\User as UserModel;
    use app\model\ucenter\UserAuth as UserAuthModel;
    use app\event\ucenter\Notice as NoticeEvent;
    use think\Exception;

    class UserAuth extends Base
    {

        /**
         * 认证审核
         * @param array     $param
         * @param UserModel $admin
         * @return mixed
         */
        public function examine($param, $admin)
        {
            try {
                if (!$admin) {
                    throw new Exception('管理不存在');
                }
                $auth = UserAuthModel::where('id', $param['id'])->where('status', 0)->find();
                if (!$auth) {
                    throw new Exception('认证信息异常');
                }
                if (1 == $auth->user['is_auth']) {
                    throw new Exception('当前用户已经认证');
                }
                $user = $auth->user;
                // 发送消息
                $message = [];
                $message['type'] = 'auth';
                $message['relation_id'] = $auth['id'];
                $message['from_user_id'] = 10000;
                // 开启事务
                Db::startTrans();
                if (1 == $param['examine']) {
                    // 通过认证;
                    $user['is_auth'] = 1;
//                    $user['auth_info'] = $auth['auth_info'];
                    $user['auth_name'] = $auth['idcard_name'];
                    $auth['status'] = 1;
                    $auth['result'] = '认证已通过';
                    $auth['idcard_name'] = $param['idcard_name'];
//                    $auth['idcard_no'] = $param['idcard_no'];
                    $data['message'] = '认证已通过';
                    if (!$user->save()) {
                        throw new Exception('认证信息保存异常');
                    }
					$admin = UserModel::where('id', Config::get('ucenter.system_notice_from_user_id'))->find();
					$log = [];
					$log['change_code'] = 'auth';
					$log['experience'] = 100;
					$log['relation_id'] = $user['id'];
					$log['remark'] = '认证通过经验值加10';
					$log['message'] = '认证通过经验值加10';
					UserExperienceEvent::change($log, $user, $admin);
                } else {
                    // 拒绝认证
                    if (!$param['result']) {
                        throw new Exception('请填写拒绝理由');
                    }
                    $auth['status'] = -1;
                    $data['message'] = '认证被拒绝';
                    $message['result'] = $param['result'];
                }
                if (!$auth->save()) {
                    throw new Exception('认证保存异常');
                }
                //                $message['join_queue'] = 0;
                NoticeEvent::send($message, $auth->user);
                // 提交事务
                Db::commit();
                // 返回订单数据
                $data['code'] = 0;
            } catch (Exception $e) {
                // 回滚事务
                Db::rollback();
                $data['message'] = $e->getMessage();
                $data['code'] = 1;
            }
            // 发送卖家通知
            return $data;
        }


        /**
         * 提交认证
         * @param array     $param
         * @param UserModel $user
         * @return mixed
         * @author 微尘 <yicmf@qq.com>
         * @datetime: 2020/7/12 21:58
         */
        public function add($param, $user = null)
        {
            try {
                if (!$user) {
                    throw new Exception('用户不存在');
                }
                // 已经认证的不再提交
                if (0 != $user['is_auth']) {
                    throw new Exception('已经认证，无需提交');
                }
                // 如果有历史提交，则置为无效
                if ($user->auths()->where('status', '>', -1)->find()) {
                    $user->auths()->where('status', '>', -1)->update(['status'=>-2]);
                }
				NoticeEvent::checkCode($param['sms_code']);
                $auth = [];
                // 完善信息
                $auth['idcard_name'] = $param['idcard_name'];
//                $auth['idcard_no'] = $param['idcard_no'];
//                $auth['front_id'] = $param['front_id'];
//                $auth['back_id'] = $param['back_id'];
//                $auth['hand_id'] = $param['hand_id'];
//                $auth['message'] = $param['message'];
                $auth = $user->auths()->save($auth);
                if (!$auth) {
                    throw new Exception('用户认证提交异常');
                }
//                if ($auth->hand && isset($auth->hand['info']['code']) && 0 == $auth->hand['info']['code']) {
//                    $auth['status'] = 1;
//                    $user['is_auth'] = 1;
//                    $user['auth_name'] = $param['idcard_name'];
//                    $data['message'] = '自动审核通过';
//                    $auth->save();
//                } elseif ($auth->hand && isset($auth->hand['info']['code']) && 0 != $auth->hand['info']['code']) {
//                    $data['remark'] = $auth->hand['info']['error_msg'];
//                    $auth->save();
//                } else {
                    $data['message'] = '用户认证已提交，请等待审核';
//                }
                // 返回订单数据
                $data['code'] = 0;
            } catch (Exception $e) {
                $data['message'] = $e->getMessage();
                $data['code'] = 1;
            }
            return $data;
        }

    }
